package com.zhp.farmerclient.controller;

import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Comment;
import com.zhp.farmerclient.entity.Post;
import com.zhp.farmerclient.service.CommentService;
import com.zhp.farmerclient.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/post")
public class PostController {

    @Autowired
    public PostService postService;
    @Autowired
    public CommentService commentService;

    //分页显示
    @GetMapping()
    public Result list(@RequestParam Integer page,
                       @RequestParam Integer size){
        return postService.list(page,size);
    }
    //部分显示
    @GetMapping("/part")
    public Result part(){
        return postService.part();
    }
    //根据用户ID查看用户帖子
    @GetMapping("/id")
    public Result check(@RequestParam Integer page,
                       @RequestParam Integer size,
                       @RequestParam(name = "user_id") Integer id){
        return postService.check(page,size,id);
    }
    //根据帖子ID删除帖子
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") int id){
        return postService.delete(id);
    }
    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) { // [1,2,3]
        return postService.deletes(ids);
    }

    //分页显示（查询）
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String title,
                           @RequestParam(defaultValue = "") String content,
                           @RequestParam(defaultValue = "") String user_id) {
        return postService.page(pageNum,pageSize,title,content,user_id);
    }

    //详情页
    @PostMapping("/postList/{id}")
    public Result detail(@PathVariable("id") int id){
        return  postService.detail(id);
    }
    //获取评论列表
    @PostMapping("/comments/{id}")
    public Result getComments(@PathVariable("id") int id){
        return commentService.getComments(id);
    }
    //点赞
    @PostMapping("/likes/{id}")
    public Result likes(@PathVariable(name = "id") int id){
        return postService.likes(id);
    }
    //添加评论
    @PostMapping("/comment")
    public Result addComments(@RequestBody Comment comment){
        return commentService.addComments(comment);
    }
    //编辑
    @PostMapping("/edit")
    public Result edit(@RequestBody Post post){
       return postService.edit(post);
    }
}
